<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test07</title>
    <script src="https://unpkg.com/vue/dist/vue.js" type="text/javascript"></script>
</head>
<body>
<script id="template01" type="text/x-template">
    <div>
        <input>
        <button>Click01</button>
    </div>
</script>
<script id="template02" type="text/x-template">
    <div>
        <input>
        <button>Click02</button>
    </div>
</script>
<!--<template id="template01">-->
<!--    <div>-->
<!--        <input>-->
<!--        <button>Click</button>-->
<!--    </div>-->
<!--</template>-->

<div id="app">
    <!--    <text-btn01></text-btn01>-->
    <!--    <text-btn02></text-btn02>-->

    <keep-alive>
        <!--        <component v-bind:is="curComp"></component>-->
        <component :is="curComp"></component>
    </keep-alive>

    {{message}}
    <button @click="click">Click</button>
</div>

<script>
    Vue.component('text-btn01', {
        template: '#template01',
        destroyed: function () {
            alert('text-btn01 destroyed')
        },
        activated: function () {
            alert('text-btn01 activated')
        },
        deactivated: function () {
            alert('text-btn01 deactivated')
        },
    });

    Vue.component('text-btn02', {
        template: '#template02',
        destroyed: function () {
            alert('text-btn02 destroyed')
        },
        activated: function () {
            alert('text-btn02 activated')
        },
        deactivated: function () {
            alert('text-btn02 deactivated')
        },
    });

    let vm = new Vue({
        el: '#app',
        data: {
            message: '',
            curComp: 'text-btn01'
        },
        methods: {
            click: function () {
                // this.message = 'Hello Vue!';

                let num = Math.random();

                if (num < 0.5) {
                    this.curComp = 'text-btn01';
                } else {
                    this.curComp = 'text-btn02';
                }
            }
        },
        // beforeCreate:function () {
        //     alert("beforeCreate")
        // },
        // created:function () {
        //     alert("created")
        // },
        // beforeMount:function () {
        //     alert("beforeMount")
        // },
        // mounted:function () {
        //     alert("mounted")
        // },
        // beforeUpdate:function () {
        //     alert("beforeUpdate")
        // },
        // updated:function () {
        //     alert("updated")
        // },
    });


</script>

</body>
</html>